package com.chrelyonly.controller;

import com.chrelyonly.util.myRes.AjaxJson;
import com.chrelyonly.util.wx.WxSendUtil;
import com.chrelyonly.util.wx.WxWebOauthUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.util.List;

/**
 * @author chrelyonly
 */
@RestController
@RequestMapping("/wx")
public class WxController {
    /**
     * 发送消息
     */
    @RequestMapping("/sendMessage")
    public AjaxJson sendMessage() throws WxErrorException {
        List<String> strings = WxSendUtil.wxMpTemplateMessage();
        return AjaxJson.success().put("OK", strings);
    }

    /**
     * 微信授权 获取code
     */
    @RequestMapping("/wxAuth")
    public AjaxJson wxAuth(HttpServletRequest httpServletRequest) throws MalformedURLException, URISyntaxException {
        return WxWebOauthUtil.wxGetCode(httpServletRequest);
    }

    /**
     * 微信授权 code 换取 userinfo
     */
    @RequestMapping("/wxGetUserInfo")
    public AjaxJson wxGetUserInfo(String code, String state, HttpServletResponse response) throws WxErrorException {
        return WxWebOauthUtil.wxGetOpenId(code, state, response);
    }
}
